package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.entity.AntiepidemicMonitor;
import com.antdesignbackend.backend.entity.entityVo.AntiepidemicMonitorVo;
import com.antdesignbackend.backend.service.AntiepidemicMonitorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/antiepidemicMonitor")
public class AntiepidemicMonitorController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    @Autowired
    AntiepidemicMonitorService antiepidemicMonitorService;
    /**
     *防疫监测
     */
    @GetMapping("/index")
    @AuthToken
    public String index(Model model) {
        model.addAttribute("animalTypes",getLoginAnimalTypes());
        return "records/antiepidemicMonitor/index";
    }
    /**
     * 表格查询
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    public ReturnVO tableSearch(@RequestBody AntiepidemicMonitorVo antiepidemicMonitorVo) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        List<AntiepidemicMonitorVo> antiepidemicMonitorVos=new ArrayList<>();
        QueryWrapper<AntiepidemicMonitorVo> wrapper =new QueryWrapper<>();
        if(antiepidemicMonitorVo.getEnclosureId()!=null&& !antiepidemicMonitorVo.getEnclosureId().equals(""))
        {
            wrapper.like("a.EnclosureId",antiepidemicMonitorVo.getEnclosureId());
        }
        if (antiepidemicMonitorVo.getTimeInterval() != null && !antiepidemicMonitorVo.getTimeInterval().equals("")){
            String[] str=antiepidemicMonitorVo.getTimeInterval().split(" - ");
            String star=str[0];
            String end=str[1];
            wrapper.between("a.samplesDate",star,end);
        }
        wrapper.eq("a.eid",getLoginEnterpriseId());
        wrapper.orderByDesc("a.samplesDate").orderByDesc("a.reportDate");
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=antiepidemicMonitorService.count(wrapper);
        result.setTotal(total);
        System.out.println("*******************************满足条件的数据有："+total);
        //再查询
        if(total>=0)
        {
            antiepidemicMonitorVos=antiepidemicMonitorService.search(wrapper,new Page<>(antiepidemicMonitorVo.getPageIndex(),antiepidemicMonitorVo.getPageSize()));
        }
        result.setData(antiepidemicMonitorVos);
        return result;
    }
    @PostMapping("/tableSearchMe/{eid}")
    @ResponseBody
    public ReturnVO tableSearchMe(@PathVariable("eid") int eid) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        List<AntiepidemicMonitorVo> antiepidemicMonitorVos=new ArrayList<>();
        QueryWrapper<AntiepidemicMonitorVo> wrapper =new QueryWrapper<>();
        wrapper.eq("a.eid",eid);
        wrapper.orderByDesc("a.samplesDate");
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=antiepidemicMonitorService.count(wrapper);
        result.setTotal(total);
        System.out.println("*******************************满足条件的数据有："+total);
        //再查询
        if(total>=0)
        {
            antiepidemicMonitorVos=antiepidemicMonitorService.search(wrapper,new Page<>(1,10));
        }
        result.setData(antiepidemicMonitorVos);
        return result;
    }
    /**
     * 根据id删除
     */
    @DeleteMapping("/deleteById/{id}")
    @ResponseBody
    public ReturnVO deleteById(@PathVariable Serializable id) {
        result = new ReturnVO();
        if (antiepidemicMonitorService.removeById(Integer.parseInt(id.toString())) == true) {
            result.setMessage("删除成功");
        } else {
            result.setMessage("删除失败");
            result.setCode("0");
        }
        return result;
    }
    /**
     * 修改/新增操作的保存
     */
    @PostMapping("/save")
    @ResponseBody
    @AuthToken
    public ReturnVO save(@RequestBody AntiepidemicMonitor antiepidemicMonitor) throws Exception {
        result = new ReturnVO();
        boolean b = false;
        if(antiepidemicMonitor.getAntiepidemicMonitorId()==null || antiepidemicMonitor.getAntiepidemicMonitorId()==0){
            antiepidemicMonitor.setUid(getLoginUserId());
            antiepidemicMonitor.setEid(getLoginEnterpriseId());
            antiepidemicMonitor.setReportDate(new Date());
            b=antiepidemicMonitorService.save(antiepidemicMonitor);
        }else{
            b=antiepidemicMonitorService.updateById(antiepidemicMonitor);
        }
        if(b==true){
            result.setMessage("保存成功！");
            result.setCode("200");
        }
        else
        {
            result.setMessage("保存失败！");
            result.setCode("500");
        }
        return result;
    }
}
